# analysis/source_analysis.py
from pyspark.sql import DataFrame

class SourceAnalyzer:
    @staticmethod
    def analyze_source_performance(df: DataFrame) -> DataFrame:
        return (df
                .groupBy("traffic_source")
                .agg({"visits": "sum",
                      "bounce_rate": "avg",
                      "avg_session_duration": "avg",
                      "conversion_rate": "avg"})
                .withColumnRenamed("sum(visits)", "total_visits")
                .withColumnRenamed("avg(bounce_rate)", "avg_bounce_rate")
                .withColumnRenamed("avg(avg_session_duration)", "avg_session_duration")
                .withColumnRenamed("avg(conversion_rate)", "avg_conversion_rate")
                .orderBy("total_visits", ascending=False))